<?
session_start();
if(!session_is_registered("myusername")){
    header("location:index.php");
}
else{
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Notation des commerces de Mons >> Vos suggestions personnalisées</title>
        <link rel="stylesheet" href="./mystyle.css" type="text/css" media="screen" />
    </head>
    <body>
       <?
        include 'myheader.php';
       ?>
        <center>
            <?php
            session_start();

            include 'connect_db.php';



            //USERID
            $sql="SELECT idusers FROM users_site WHERE email_adress='$myusername'";
            $result1=mysql_query($sql);
            $count=mysql_num_rows($result1);
            if($count==1){
                while($tableau1=mysql_fetch_array($result1,MYSQL_ASSOC)){
                    $userid =   $tableau1['idusers'];
                    //echo "userID:";
                    //echo $userid;
                }
            }

            //NUMBER OF ITEMS IN THE EXPECTED TABLE
            $sql="SELECT COUNT(idusers) as n FROM ratings_expected";
            $result_rx=mysql_query($sql);
            if($result_rx){
                $count=mysql_num_rows($result_rx);
            }
            else{
                $count=0;
            }
            if($count==1){
                while($tableau1=mysql_fetch_array($result_rx,MYSQL_ASSOC)){
                    $n_rx   =   intval($tableau1['n']);
                    //echo $n_rx;
                }
            }
            //NUMBER OF ITEMS IN THE EXPECTED TABLE
            $sql="SELECT COUNT(iditems) as n FROM items";
            $result_it=mysql_query($sql);
            if($result_it){
                $count=mysql_num_rows($result_it);
            }
            else{
                $count=0;
            }
            
            if($count==1){
                while($tableau1=mysql_fetch_array($result_it,MYSQL_ASSOC)){
                    $n_it   =   intval($tableau1['n']);
                    //echo $n_it;
                }
            }

            echo "<h2>Nos suggestions:</h2>";
            if($n_it==$n_rx){
                echo "<h4>(basées sur notre algorithme)</h4>";
                //$sql="SELECT ";
            }
            else{
                echo "<h4>(basées sur une méthode simple: revenez plus tard pour voir vos résultats personnalisés!)</h4>";
            }


            $sql="SELECT * FROM type ORDER BY idtype";
            $result=mysql_query($sql);
            //echo $result;


            while($tableau=mysql_fetch_array($result,MYSQL_ASSOC)){
                echo "<div id='colonne1' align='center' class='colonnem'>";
                echo '<h2>', $tableau['name'], '</h2>';
                //$sql="select items.iditems,items.name,items.street,items.number,cities.zip,cities.name as 'cityname' from items inner join cities on items.cities_idcities=cities.idcities WHERE type_idtype=$tableau[idtype]";
                //echo $tableau['idtype'];
                if($n_it==$n_rx){
                    $sql="SELECT ratings_expected.rating as 'sum_r',items.name,items.street,items.number,cities.zip,cities.name as 'cityname'
                                        from items
                                        INNER JOIN ratings_expected ON ratings_expected.items_iditems=items.iditems
                                        INNER JOIN cities ON cities.idcities=items.cities_idcities
                                        WHERE items.type_idtype=".$tableau['idtype']." AND ratings_expected.users_idusers=".$userid.
                                        " ORDER BY ratings_expected.rating DESC";
                }
                else{
                    $sql="SELECT SUM(ratings.rating) as 'sum_r',items.name,items.street,items.number,cities.zip,cities.name as 'cityname'
                                        FROM items
                                        INNER JOIN ratings ON ratings.items_iditems=items.iditems
                                        INNER JOIN cities ON cities.idcities=items.cities_idcities
                                        WHERE items.type_idtype=".$tableau['idtype'].
                                        " GROUP BY items.iditems
                                        ORDER BY SUM(ratings.rating) DESC";
                }
                $result1=mysql_query($sql);
                //echo $result1;

                echo '<TABLE BORDER=0>';
                echo '<TR><TD><b>Indicateur</b></TD><TD><b>Nom</b></TD><TD><b>Adresse</b></TD><TD><b>Code postal</b></TD><TD><b>Ville</b></TD></TR>';
                while($tableau1=mysql_fetch_array($result1,MYSQL_ASSOC)){
                    echo '<TR><TD>',$tableau1['sum_r'],'</TD><TD>',$tableau1['name'],'</TD><TD><small>',$tableau1['street'],',',$tableau1['number'],'</small></TD><TD><small>',$tableau1['zip'],'</small></TD><TD><small>',$tableau1['cityname'],'</small></TD>';
                }
                echo '</TABLE>';
                echo '</div>';
            }

            mysql_close($link);
            ?>
        </center>
    </body>
</html>
